package com.lean.pulltorefresh.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import com.lean.pulltorefresh.R;
import com.lean.pulltorefresh.view.base.BaseDrawable;

/**
 * Created by Lean·He on 16/7/28.
 */
public class HorizontalProgressDrawable extends View implements BaseDrawable {

    private static final int ANIMATION_TIME = 2000;
    private boolean isRunning;


    private int screenWidth;
    private int screenHeight;
    private int curLength;
    private Paint paint;
    private ValueAnimator pullanimator;

    public HorizontalProgressDrawable(Context context) {
        this(context, null);
    }

    public HorizontalProgressDrawable(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalProgressDrawable(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(getResources().getColor(R.color.default_progress_color));
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        paint.setStyle(Paint.Style.FILL);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        int centerWidth = screenWidth / 2;
        canvas.drawRect(centerWidth - curLength / 2, 0, centerWidth + curLength / 2, getMeasuredHeight(), paint);
        canvas.restore();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void start() {
        if (!isRunning) {
            paint.setColor(getResources().getColor(R.color.default_progress_color));
            isRunning = true;
            pullanimator = ValueAnimator.ofFloat(0, 1, 0);
            pullanimator.setRepeatCount(ValueAnimator.INFINITE);
            pullanimator.setRepeatMode(ValueAnimator.RESTART);
            pullanimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float percent = (float) animation.getAnimatedValue();
                    curLength = (int) (percent * screenWidth);
                    paint.setColor(getResources().getColor(R.color.default_progress_color));
                    invalidate();
                }
            });
            pullanimator.setDuration(ANIMATION_TIME);
            pullanimator.start();
        }
    }

    public void stop() {
        if (isRunning) {
            isRunning = false;
            if (pullanimator != null) {
                pullanimator.setDuration(0);
                pullanimator.cancel();
                pullanimator = null;
            }
        }
    }

    public void show() {
        start();
        setVisibility(VISIBLE);
    }

    public void dismiss() {
        stop();
        setVisibility(GONE);
    }

    @Override
    public float getProgress() {
        return curLength;
    }

    public void setProgress(float percent) {
        setVisibility(VISIBLE);
        if (isRunning) {
            stop();
        }
        if (percent > 1.0f) {
            percent = Math.min(1 - (percent - 1.0f), 2.0f);
            curLength = (int) (percent * screenWidth);
            curLength = Math.max(curLength, screenWidth / 40);
            paint.setColor(getResources().getColor(R.color.default_progress_color));
        } else {
            curLength = (int) (percent * screenWidth);
            paint.setColor(Color.RED);
        }
        invalidate();
    }
}

